package cn.wolfcode.p2p.business.domain;

import cn.wolfcode.p2p.base.domain.BaseDomain;
import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.util.Constant;
import com.alibaba.fastjson.JSON;
import lombok.Getter;
import lombok.Setter;
import org.springframework.format.annotation.DateTimeFormat;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

/**
 * 借款对象
 */
@Setter
@Getter
public class BidRequest extends BaseDomain {
    private Long id;

    /**
     * 版本
     */
    private Integer version = 0;
    /**
     * 还款方式
     */
    private Integer returnType = Constant.RETURN_TYPE_MONTH_INTEREST_PRINCIPAL;
    /**
     *  借款类型
     */
    private Integer bidRequestType = Constant.BIDREQUEST_TYPE_NORMAL;
    /**
     * 借款的状态
     */
    private Integer bidRequestState;
    /**
     * 借款金额
     */
    private BigDecimal bidRequestAmount = Constant.ZERO;;
    /**
     * 借款利率
     */
    private BigDecimal currentRate;
    /**
     * 还款月份
     */
    private Integer monthes2Return;
    /**
     * 投标次数
     */
    private Integer bidCount = 0;
    /**
     * 总回报金额
     */
    private BigDecimal totalRewardAmount = Constant.ZERO;;
    /**
     * 当前投资金额
     */
    private BigDecimal currentSum = Constant.ZERO;;
    /**
     * 借款标题
     */
    private String title;
    /**
     * 借款描述
     */
    private String description;
    /**
     * 风控评审意见
     */
    private String note;
    /**
     * 招标到期时间
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date disableDate;
    /**
     * 借款人
     */
    private LoginInfo createUser;
    /**
     * 招标天数
     */
    private Byte disableDays;
    /**
     * 最小投标金额
     */
    private BigDecimal minBidAmount;
    /**
     * 借款申请时间
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date applyTime;
    /**
     * 借款发布时间
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date publishTime;

    /**
     * 投标记录
     */
    List<Bid> bids = new ArrayList<>();

    /**
     * 获取投标进度
     */
    public int getPersent(){
        return currentSum.divide(bidRequestAmount,Constant.CAL_SCALE,RoundingMode.HALF_UP).multiply(new BigDecimal("100")).intValue();
    }

    /**
     * 还需借款金额
     */
    public BigDecimal getRemainAmount(){
        return bidRequestAmount.subtract(currentSum);
    }

    /**
     * 增加投标数
     */
    public void addBidCount(){
        this.bidCount += 1;
    }

    /**
     * 增加当前总投资金额
     * @param amount 投资金额
     */
   public void addCurrentSum(BigDecimal amount){
        BigDecimal temp = this.currentSum.add(amount);
        this.currentSum = temp;
   }


    /**
     * json
     */
    public String getJsonString(){
        Map map = new HashMap<>();
        map.put("id",getId());
        map.put("username",createUser.getUsername());
        map.put("title",title);
        map.put("bidRequestAmount",bidRequestAmount);
        map.put("currentRate",currentRate);
        map.put("monthes2Return",monthes2Return);
        map.put("returnType",getReturnTypeName());
        map.put("totalRewardAmount",totalRewardAmount);
        map.put("bidRequestType",bidRequestType);
        return JSON.toJSONString(map);
    }

    public String getReturnTypeName(){
        return returnType == Constant.RETURN_TYPE_MONTH_INTEREST_PRINCIPAL?"按月分期还款":"按月到期";
    }

    public String getBidRequestStateName(){
        switch (bidRequestState){
            case  Constant.BIDREQUEST_STATE_PUBLISH_STATE_APPLY: return "待审核";
            case  Constant.BIDREQUEST_STATE_PUBLISH_PENDING: return "待发布";
            case  Constant.BIDREQUEST_STATE_BIDDING: return "招标中";
            case  Constant.BIDREQUEST_STATE_UNDO: return "已撤销";
            case  Constant.BIDREQUEST_STATE_BIDDING_OVERDUE: return "流标";
            case  Constant.BIDREQUEST_STATE_APPROVE_PENDING_1: return "满标1审";
            case  Constant.BIDREQUEST_STATE_APPROVE_PENDING_2: return "满标2审";
            case  Constant.BIDREQUEST_STATE_REJECTED: return "满标审核被拒绝";
            case  Constant.BIDREQUEST_STATE_PAYING_BACK: return "还款中";
            case  Constant.BIDREQUEST_STATE_COMPLETE_PAY_BACK: return "已还清";
            case  Constant.BIDREQUEST_STATE_PAY_BACK_OVERDUE: return "逾期";
            case Constant.BIDREQUEST_STATE_PUBLISH_REFUSE: return "发标审核拒绝状态";
        }
        return "状态异常";
    }
    public String getBidRequestTypeDisplay(){
        switch (bidRequestType){
            case  Constant.BIDREQUEST_TYPE_NORMAL: return "信用标";
            case  Constant.BIDREQUEST_TYPE_CREDITTRANSFER: return "债权标";
            case  Constant.EXPERIENCE_BIDREQUEST_TYPE_CREDITTRANSFER: return "体验标";
        }
        return "类型异常";
    }

    /**
     * 覆写比较方法
     */
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        BidRequest that = (BidRequest) o;
        return Objects.equals(id, that.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}